import React, {Component} from 'react';

import './index.css'


export default class Count extends Component {

    // 加
    increment = () => {
        const {value} = this.selectValue;
        this.props.jia(value * 1);
    }

    // 减
    decrement = () => {
        const {value} = this.selectValue;
        this.props.jian(value * 1);
    }

    // 奇数时加
    incrementIfOdd = () => {
        const {value} = this.selectValue;
        if (this.props.count % 2 !== 0) {
            this.props.jia(value * 1);
        }
    }

    // 异步加
    incrementAsync = () => {
        const {value} = this.selectValue;
        this.props.jiaAsync(value * 1, 1000);
    }


    render() {
        // console.log(this.props);
        return (
            <div>
                <h1>当前求和为: {this.props.count}</h1>
                <select className="sel" ref={c => this.selectValue = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>

                <div className="btns">
                    <button onClick={this.increment}>+</button>
                    <button onClick={this.decrement}>-</button>
                    <button onClick={this.incrementIfOdd}>当前为奇数时加</button>
                    <button onClick={this.incrementAsync}>异步+</button>
                </div>
            </div>
        );
    }
}